package com.dream.xiaobo.controller;


import com.dream.xiaobo.entity.PageBean;
import com.dream.xiaobo.entity.Product;
import com.dream.xiaobo.entity.Type;
import com.dream.xiaobo.service.impl.ProductServiceImpl;
import com.dream.xiaobo.service.impl.TypeServiceImpl;
import com.dream.xiaobo.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

/**
 * @author xiaobo
 * @date 2021/8/31 - 13:03
 */
@WebServlet("/product")
public class ProductController extends BaseServlet{

    //创建ProductService实例化对象
    private ProductServiceImpl productService = new ProductServiceImpl();
    //创建TypeService实例化对象
    private TypeServiceImpl typeService = new TypeServiceImpl();

    /**
     * 商品列表模块
     * @param request
     * @param response
     * @return
     */
    public String show(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //默认当前页是第一页
        Integer page = 1;
        //每页显示商品数量
        Integer pageSize = 4;

        //获取到商品类别Id
        String tId = request.getParameter("tid");
        //获取到传来的当前页的页码
        String currentPage = request.getParameter("currentPage");

        //如果当前页不为空将他复制给当前页
        if(currentPage != null){
            page = Integer.parseInt(currentPage);
        }
        //调用查询页码逻辑
        PageBean<Product> pages = productService.findPage(tId, page, pageSize);
        //将查询到的页码相关信息传回去
        request.setAttribute("pageBean",pages);
        //跳转到商品列表页面
        return Constants.FOR_WARD + "/goodsList.jsp";
    }

    /**
     * 商品详情模块
     * @param request
     * @param response
     * @return
     * @throws SQLException
     */
    public String detail(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //获取商品Id
        String pId = request.getParameter("pid");
        //获取商品类型Id
        String tid = request.getParameter("tid");
        //调用商品信息逻辑方法
        Product product = productService.findProductByPId(pId);
        //调用商品分类逻辑方法
        Type type = typeService.findByTid(tid);
        //将查询到的内容返回页面
        request.setAttribute("type",type);
        //将查询到的内容返回页面
        request.setAttribute("product",product);
        //跳转到商品详情页面
        return Constants.FOR_WARD + "/goodsDetail.jsp";
    }

}
